<?php
class control_article extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_article();
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$rs = $model->select("","","","order by aid desc");
		$params['article'] = $rs->items;
		$params['pageSize'] = $rs->pageSize;
		$params['page'] = $page;
		$params['article_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/article_list.php', $params);
	}
	
	function show(){
		$aid = intval($_REQUEST['aid']);
		if($aid > 0){
			$mArticle = new core_model_article($aid);
			$params['article'] = $mArticle->get();
			$msg = "编辑";
			$m = 'edit';
		}else{
			$mArticle = new core_model_article();
			$msg = "添加";
			$m = 'create';
		}
		if($_POST){
			$post = Qutil::filter($_POST);
			if($post['token'] != md5($_COOKIE['adminKey'])){
				$this->showMsg("非法请求",2);
			}
			$post['view'] = intval($post['view']);
			$post['from'] = $post['from'] ? $post['from'] : "本站";
			if($mArticle->$m($post) === false){
				$this->showMsg($msg."出错",2);
			}
			$model = new core_model_adminlog();
			$model->addLog(4, "{$msg}了文章：“{$mArticle->getData('title')}”");
			$this->showMsg($msg."成功",1,Qtpl::createUrl('article', 'index','','admin'));
		}
		$params['hname'] = $msg;
		$params['article_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/article.php', $params);
	}
	
	function del(){
		$aid = intval($_REQUEST['aid']);
		if($aid <=0 ) {
			$this->showMsg("删除出错",2);
		}
		if($_GET['token'] != md5($_COOKIE['adminKey'])){
			$this->showMsg("非法请求",2);
		}
		$mCate = new core_model_article($aid);
		$n = $mCate->getData('title');
		$mCate->del();
		$model = new core_model_adminlog();
		$model->addLog(4, "删除了文章：“{$n}”");
		$this->showMsg("删除成功",1,Qtpl::createUrl('article', 'index','','admin'));
	}
}